/**
 * Project Name:b2c-app
 * File Name:UploadController.java
 * Package Name:cn.bluemobi.app.controller.file
 * Date:2016年1月22日下午4:02:17
 * Copyright (c) 2016, bluemobi.cn All Rights Reserved.
 *
 */

package cn.bluemobi.app.controller.file;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.bluemobi.app.core.controller.BaseController;
import cn.bluemobi.app.core.utils.AppConstants;
import cn.bluemobi.app.dto.file.FileDto;
import cn.bluemobi.app.utils.FileUtils;
import cn.bluemobi.common.core.dto.Dto;
import cn.bluemobi.common.core.dto.Page;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

/**
 * Description: <br/>
 * Date: 2016年1月22日 下午4:02:17 <br/>
 * 
 * @author hut
 * @version
 * @see
 */
@Controller
@RequestMapping("/upload")
@Api(description = "文件上传", value = "upload")
public class UploadController extends BaseController {

    @ApiOperation(value = "1.上传图片", httpMethod = "POST")
    @RequestMapping("/uploadImg")
    @ResponseBody
    public Dto<FileDto> uploadImg(@ApiParam("会员ID") @RequestParam("memberId") String memberId,
            @ApiParam("目录") @RequestParam("dir") String dir, @RequestParam("file") MultipartFile file,
            HttpServletRequest request) throws IllegalStateException, IOException {
        FileDto dto = new FileDto();
        Dto<FileDto> ret = new Dto<FileDto>(dto);
        if (file != null && !file.isEmpty()) {
            String fileName = file.getOriginalFilename().toLowerCase();
            if (!FileUtils.checkImgTypes(fileName)) {
                ret.errorMsg("请上传" + StringUtils.join(FileUtils.IMAGE_FORMAT_ARRAY, "、") + "格式的图片");
                return ret;
            }
            if (file.getSize() > FileUtils.IMAGE_MAX_SIZE) {
                ret.errorMsg("请上传文件大小小于" + FileUtils.IMAGE_MAX_SIZE / 1024 / 1024 + "m的图片");
                return ret;
            }
            // 相对路径
            String imgPath = dir + File.separator + UUID.randomUUID() + fileName.substring(fileName.lastIndexOf("."));
            dto.setRelativePath(imgPath.replace('\\', '/'));
            // url
            StringBuilder newFileName = new StringBuilder(AppConstants.IMG_DISK_PATH);
            newFileName.append(File.separator).append(imgPath);
            File newFile = new File(newFileName.toString());
            if (!newFile.exists()) {
                newFile.mkdirs();
            }
            file.transferTo(newFile);
            dto.setUrl(AppConstants.IMG_CONTEXT_PATH + "/" + dto.getRelativePath());
        }
        return ret;
    }

    @ApiOperation(value = "2、上传多张图片", httpMethod = "POST")
    @RequestMapping("/uploadImgList")
    @ResponseBody
    public Page<FileDto> uploadImgList(@ApiParam("会员ID") @RequestParam("memberId") String memberId,
            @ApiParam("目录") @RequestParam("dir") String dir, @RequestParam("files") List<MultipartFile> files,
            HttpServletRequest request) throws IllegalStateException, IOException {
        List<FileDto> fileDtoList = new ArrayList<FileDto>();
        Page<FileDto> ret = new Page<FileDto>(fileDtoList);
        if (files.size() > 0) {
            for (int i = 0; i < files.size(); i++) {
                MultipartFile file = files.get(i);
                FileDto dto = new FileDto();
                if (file != null && !file.isEmpty()) {
                    String fileName = file.getOriginalFilename().toLowerCase();
                    System.err.println("fileName=" + fileName);
                    if (!FileUtils.checkImgTypes(fileName)) {
                        ret.errorMsg("请上传" + StringUtils.join(FileUtils.IMAGE_FORMAT_ARRAY, "、") + "格式的图片");
                        return ret;
                    }
                    // if (file.getSize() > FileUtils.IMAGE_MAX_SIZE) {
                    // ret.errorMsg("请上传文件大小小于" + FileUtils.IMAGE_MAX_SIZE /
                    // 1024 / 1024 + "m的图片");
                    // return ret;
                    // }
                    System.err.println("dir=" + dir);
                    // 相对路径
                    String imgPath = dir + File.separator + UUID.randomUUID()
                            + fileName.substring(fileName.lastIndexOf("."));
                    System.err.println("imgPath=" + imgPath);
                    dto.setRelativePath(imgPath.replace('\\', '/'));
                    // url
                    StringBuilder newFileName = new StringBuilder(AppConstants.IMG_DISK_PATH);
                    System.err.println("newFileName=" + newFileName);
                    newFileName.append(File.separator).append(imgPath);
                    File newFile = new File(newFileName.toString());
                    if (!newFile.exists()) {
                        newFile.mkdirs();
                    }
                    file.transferTo(newFile);
                    dto.setUrl(AppConstants.IMG_CONTEXT_PATH + "/" + dto.getRelativePath());
                    fileDtoList.add(dto);
                }
            }
        }

        return ret;
    }
}
